<?php

use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\service\GoodsTestActivity\FineGoodsService;

/**
 * 活动申请详情
 * Class ListAction
 */
class ApplyInfoAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;
    private const PAGE_SIZE = 20;

    public function main()
    {


        $id = $this->getRequest()->getPost('id', 0);
        $uid = $this->uid;
        if (!$id || !$uid) {
            throw new \Exception('非法提交！');
        }
        //活动信息
        $data = $this->_getDetails($id);

        $end_time = $data['schedule_end_time'];
        $start_time = $data['schedule_start_time'];
        $now = time();
        if ($now < $start_time || $now >= $end_time) {
            throw new \Exception('该活动还不可以申请！');
        }
        $this->setSuccess($data);
    }

    /**
     * 活动信息
     * @param $id
     * @return array
     * @throws Exception
     */
    private function _getDetails($id): array
    {
        //获取详情
        $field = [
            'id',
            'type',
            'brand_id',
            'brand_name',
            'basic_title',
            'activity_title',
            'goods_num',
            'goods_worth',
            'goods_max_worth',
            'schedule_start_time',
            'schedule_end_time',
            'activity_state',
            'cover_img',
            'advantage_img',
            'advantage_text',
            'category_tag_id',
        ];
        $details = FineGoodsModel::first(['id' => $id], $field);
        if (!$details) {
            throw new Exception("该活动不存在！");
        }
        //获取感兴趣住友
        $details = FineGoodsService::getFineGoodsPv([$details], false);
        return $details ? $details[0] : [];
    }

}


